/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.inventory;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import main.ServicesList;
import model.inventory.Dept;
import model.inventory.Item;
import model.inventory.Principal;
import model.publics.Supplier;
import org.apache.log4j.Logger;
import utility.Constants;
import utility.NameValueList;
import utility.com.ComponentUtils;
import utility.com.treeTable.AbstractTreeTableModel;
import utility.com.treeTable.TreeTable;
import utility.com.treeTable.TreeTableCellListener;
import utility.typeSafe.EnumUtils;
import utility.typeSafe.ItemPerfoma;
import view.RekapTemplate;
import view.lookup.LookupDeptFrm;
import view.lookup.LookupSimpleFrm;

/**
 *
 * @author jay
 */
public class DivisiOrderFrm extends RekapTemplate {

    private TreeTable treeTable;
    private static Logger log = Logger.getLogger(DivisiOrderFrm.class);
    
    private AbstractTreeTableModel treeTableModel;
    private Supplier supplier;
    private Principal principal;
    private Dept dept;
    
    private List<NameValueList> performList = EnumUtils.enumToNameValueList(ItemPerfoma.class);
    private Object oldValue, newValue;
    private ItemListener comboboxListener = new ComboboxListener();
    private TreeTableCellListener tableCellListener;
    
    public DivisiOrderFrm() {
        initComponents();
        txtBarcode.addKeyListener(new ActionKeyListener());
        txtPLU.addKeyListener(new ActionKeyListener());
        txtNama.addKeyListener(new ActionKeyListener());
        txtKategori.addKeyListener(new ActionKeyListener());
        txtPrincipal.addKeyListener(new ActionKeyListener());
        txtSupplier.addKeyListener(new ActionKeyListener());
        txtBarcode.addFocusListener(new ActionFocusGained());
        txtPLU.addFocusListener(new ActionFocusGained());
        txtNama.addFocusListener(new ActionFocusGained());
        txtKategori.addFocusListener(new ActionFocusGained());
        txtPrincipal.addFocusListener(new ActionFocusGained());
        txtSupplier.addFocusListener(new ActionFocusGained());
    }
         
    private void createNode() {
        List<Item> tree = ServicesList.getItemService().createDivParent(getFilter());
        List<Item> rootNodes = new ArrayList<>();
         
        for (Item item : tree){
            List<Item> children = ServicesList.getItemService().getItemTree(item.getDivOrder(), getFilter());
            rootNodes.add(new Item(item.getId(), item.getPlu(), "", "", "", children));            
        }
        
        Item root = new Item(0, "DIVISI ORDER", "", "", "", rootNodes);
        
        treeTableModel = new DivisiOrderTM(root);
        treeTable = new TreeTable(treeTableModel);
        treeTable.getColumnModel().getColumn(0).setMaxWidth(150);
        treeTable.getColumnModel().getColumn(0).setPreferredWidth(150);
        treeTable.getColumnModel().getColumn(1).setMaxWidth(50);
        treeTable.getColumnModel().getColumn(1).setPreferredWidth(50);
        treeTable.getColumnModel().getColumn(3).setMaxWidth(250);
        treeTable.getColumnModel().getColumn(3).setPreferredWidth(250);
        treeTable.getColumnModel().getColumn(4).setPreferredWidth(0);
        //treeTable.getColumnModel().getColumn(4).setMaxWidth(1);
        //treeTable.getColumnModel().getColumn(4).setMinWidth(0);
        TableColumn cm = treeTable.getColumnModel().getColumn(4);
        treeTable.removeColumn(cm);
        
//        treeTable.setTreeSelectionListener(new TreeSelectionListener() {
//            @Override
//            public void valueChanged(TreeSelectionEvent e) {
//                
//                int row = treeTable.getSelectedRow();
//                int id = Integer.valueOf(treeTable.getModel().getValueAt(row, 4).toString());
//                selectedNode = ServicesList.getItemService().getItem(id);
//                if(selectedNode!=null) {
//                    initForm();
//                    txtInputDivisi.setText(selectedNode.getDivOrder());
//                    txtInputPlu.setText(selectedNode.getPlu());
//                    txtOutputNama.setText(selectedNode.getNama());
//                }
//            }
//        });
        
        //look style
        final TableCellRenderer header = treeTable.getTableHeader().getDefaultRenderer();
        treeTable.getTableHeader().setDefaultRenderer(new TableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                JLabel lbl = (JLabel) header.getTableCellRendererComponent(table, 
                      value, isSelected, hasFocus, row, column);
                lbl.setBorder(BorderFactory.createEmptyBorder(5,1,5,1));   
                lbl.setForeground(Color.WHITE);
                lbl.setBackground(Color.DARK_GRAY);
                return lbl;
            }
        });
        treeTable.setGridColor(Color.LIGHT_GRAY);
        
        tableCellListener = new TreeTableCellListener(treeTable, TreeAction);
        
        jScrollPane1.setViewportView(treeTable);
     };
      
    private void lookupMaster(int purpose, String param) {
        Object object;
        if (purpose==Constants.LOOKUP_DEPT) {
            dept = (Dept) new LookupDeptFrm().execute();
        } else if (purpose==Constants.LOOKUP_PRINCIPAL) {
            principal = (Principal) new LookupSimpleFrm().execute(purpose, param);
        } else if (purpose==Constants.LOOKUP_SUPPLIER) {
            supplier = (Supplier) new LookupSimpleFrm().execute(purpose, param);
        }
                
        setMaster(purpose);
    }
    
    private void setMaster(int purpose) {
        switch (purpose) {
            case Constants.LOOKUP_DEPT:
                if(dept!=null) {
                    txtKategori.setText(dept.getNama());
                    loadData();
                }break;
                                        
            case Constants.LOOKUP_PRINCIPAL:
                if(principal!=null) {
                    txtPrincipal.setText(principal.getNama());
                    loadData();
                }break;
                    
            case Constants.LOOKUP_SUPPLIER:
                if(supplier!=null) {
                    txtSupplier.setText(supplier.getNama());
                    loadData();
                } break;
            default: break;    
        }
    }
    
    private void getMaster(int purpose, String kode) {
        switch (purpose) {
           case Constants.LOOKUP_DEPT:
               dept = ServicesList.getDeptService().getDept(kode);
               if (dept != null) { setMaster(purpose);
               }else { lookupMaster(purpose, txtKategori.getText()); }
               break;
               
           case Constants.LOOKUP_SUPPLIER:
               supplier = ServicesList.getSupplierService().getSupplier(kode);
               if (supplier != null) { setMaster(purpose);
               }else { lookupMaster(purpose, txtSupplier.getText()); }
               break;
               
           case Constants.LOOKUP_PRINCIPAL:
               principal = ServicesList.getPrincipalService().getPrincipal(kode);
               if (principal != null) { setMaster(purpose);
               }else { lookupMaster(purpose, txtPrincipal.getText()); }
               break;
           default: break;    
        }
    }
    
    class ActionKeyListener implements KeyListener {
        @Override public void keyTyped(KeyEvent e) {} 
        @Override public void keyReleased(KeyEvent e) {}
        
        @Override public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) { 
                case KeyEvent.VK_F2: 
                    switch(e.getComponent().getName()) {
                        case "txtKategori": lookupMaster(Constants.LOOKUP_DEPT, txtKategori.getText()); break;
                        case "txtPrincipal": lookupMaster(Constants.LOOKUP_PRINCIPAL, txtPrincipal.getText()); break;
                        case "txtSupplier": lookupMaster(Constants.LOOKUP_SUPPLIER, txtSupplier.getText()); break;
                    };
                break;  
                    
                case KeyEvent.VK_ENTER: 
                    switch(e.getComponent().getName()) {
                        case "txtBarcode": 
                            newValue = txtBarcode.getText();
                            if (!newValue.equals(oldValue)) { loadData(); }
                            break;
                            
                        case "txtNama": 
                            newValue = txtNama.getText();
                            if (!newValue.equals(oldValue)) { loadData(); }
                            break;
                            
                        case "txtPLU": 
                            newValue = txtPLU.getText();
                            if (!newValue.equals(oldValue)) { loadData(); }
                            break;    
                        
                        case "txtKategori": 
                            newValue = txtKategori.getText();
                            if (!newValue.equals(oldValue)) { getMaster(Constants.LOOKUP_DEPT, txtKategori.getText()); }
                            break;
                            
                        case "txtPrincipal": 
                            newValue = txtPrincipal.getText();
                            if (!newValue.equals(oldValue)) { getMaster(Constants.LOOKUP_PRINCIPAL, txtPrincipal.getText()); }
                            break;   
                            
                        case "txtSupplier": 
                            newValue = txtSupplier.getText();
                            if (!newValue.equals(oldValue)) { getMaster(Constants.LOOKUP_SUPPLIER, txtSupplier.getText()); }
                            break;       
                    };
                break;      
            }
        }
    }
    
    class ActionFocusGained implements FocusListener {
        @Override public void focusGained(FocusEvent e) { 
            Component componen = (Component) e.getSource(); 
            if (componen instanceof JTextField) { oldValue = ((JTextField) componen).getText();
            }else if (componen instanceof JComboBox) { oldValue = ((JComboBox) componen).getSelectedIndex();
            }else if (componen instanceof JCheckBox) { oldValue = ((JCheckBox) componen).isSelected();
            }else if (componen instanceof JTextArea) { oldValue = ((JTextArea) componen).getText(); }
        }
        
        @Override public void focusLost(FocusEvent e) {
            Component componen = (Component) e.getSource(); 
            if (componen instanceof JTextField) { newValue = ((JTextField) componen).getText();
            }else if (componen instanceof JComboBox) { newValue = ((JComboBox) componen).getSelectedIndex();
            }else if (componen instanceof JCheckBox) { newValue = ((JCheckBox) componen).isSelected();
            }else if (componen instanceof JTextArea) { oldValue = ((JTextArea) componen).getText(); }
        }
    }
    
    class ComboboxListener implements ItemListener {
        @Override
        public void itemStateChanged(ItemEvent e) {
            if(e.getStateChange()==ItemEvent.SELECTED) {
                loadData();
            }
        }
    }
    
    Action TreeAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            TreeTableCellListener tblCellListener = (TreeTableCellListener)e.getSource();
            int row = tblCellListener.getRow();
                
            Object oldVal = tblCellListener.getOldValue();
            Object newVal = tblCellListener.getNewValue();
            
            if(oldVal==null) {oldVal="";}
            if(newVal==null) {newVal="";}
                
            if (!oldVal.equals(newValue)) {
                int id = Integer.valueOf(treeTable.getModel().getValueAt(row, 4).toString());
                Item item = ServicesList.getItemService().getItem(id);
                if (item!=null) {
                    item.setDivOrder(newVal.toString());
                    boolean saved = saveData(item);
                    if (!saved) {treeTable.getModel().setValueAt(oldVal, row, 1);}
                }
            }
        }
    };     
     
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jButton5 = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        txtSupplier = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtKategori = new javax.swing.JTextField();
        cmbTag = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtPrincipal = new javax.swing.JTextField();
        txtNama = new javax.swing.JTextField();
        txtPLU = new javax.swing.JTextField();
        txtBarcode = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setMaximumSize(new java.awt.Dimension(32767, 20));
        jToolBar1.setSize(this.getWidth(), 50);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reload.png"))); // NOI18N
        jButton5.setText("Load");
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton5);

        btnReset.setText("Reset");
        btnReset.setFocusable(false);
        btnReset.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnReset.setName("btnReset"); // NOI18N
        btnReset.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        jToolBar1.add(btnReset);

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/print.png"))); // NOI18N
        jButton4.setText("Print");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton4);

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        txtSupplier.setName("txtSupplier"); // NOI18N

        jLabel2.setText("Supplier :");

        jLabel1.setText("Kategori :");

        txtKategori.setName("txtKategori"); // NOI18N

        cmbTag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbTag.setName("cmbTag"); // NOI18N

        jLabel7.setText("Tag :");

        jLabel3.setText("Principal :");

        txtPrincipal.setName("txtPrincipal"); // NOI18N

        txtNama.setName("txtNama"); // NOI18N

        txtPLU.setName("txtPLU"); // NOI18N

        txtBarcode.setName("txtBarcode"); // NOI18N

        jLabel5.setText("Barcode :");

        jLabel4.setText("PLU :");

        jLabel6.setText("Nama :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtKategori, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmbTag, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtPLU, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBarcode, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(txtBarcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtPLU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtKategori, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cmbTag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel7)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jSplitPane1.setTopComponent(jPanel1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 659, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
        );

        jSplitPane1.setRightComponent(jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        loadData();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        initFilter();
    }//GEN-LAST:event_btnResetActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReset;
    private javax.swing.JComboBox cmbTag;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField txtBarcode;
    private javax.swing.JTextField txtKategori;
    private javax.swing.JTextField txtNama;
    private javax.swing.JTextField txtPLU;
    private javax.swing.JTextField txtPrincipal;
    private javax.swing.JTextField txtSupplier;
    // End of variables declaration//GEN-END:variables

    @Override
    public void execute(boolean withVisible, int id) {
        initGrid();
        performList.add(0, new NameValueList("ALL", "All"));
        cmbTag.setModel(ComponentUtils.getDefaultComboBoxModel(performList));
         if (withVisible) {
            setVisible(true);
        }
    }

    @Override
    protected void initGrid() {
    }

    public Map<NameValueList, Object> getFilter(){
        Map<NameValueList, Object> params = new HashMap<>();
        if(!"".equals(txtNama.getText())) { 
            params.put(new NameValueList("item.nama", Constants.OPERATOR_LIKE), "%"+txtNama.getText()+"%"); }

        if(!"".equals(txtPLU.getText())) { 
            params.put(new NameValueList("item.plu", Constants.OPERATOR_EQUAL), txtPLU.getText()); }
        
        if(!"".equals(txtBarcode.getText())) { 
            params.put(new NameValueList("item.barcode", Constants.OPERATOR_EQUAL), txtBarcode.getText()); }
         
        if(principal!=null) { 
            params.put(new NameValueList("item.principal.id", Constants.OPERATOR_EQUAL), principal.getId()); }
         
        if(dept!=null) { 
            params.put(new NameValueList("item.dept.id", Constants.OPERATOR_EQUAL), dept.getId()); }
        
        String tag = ((NameValueList) cmbTag.getSelectedItem()).getKey();
        if (!"ALL".equals(tag)) {
             params.put(new NameValueList("item.performa", Constants.OPERATOR_EQUAL), tag); }
         
        if(supplier!=null) { 
            params.put(new NameValueList(null, Constants.OPERATOR_CUSTOM), 
            "(item.id in (select it.id.itemId from ItemSupplier it where item.id = it.id.itemId and it.id.supplierId="+supplier.getId()+"))");
        }
        
        //params.put(new NameValueList(null, Constants.OPERATOR_ORDERBY), "item.nama"); 
         
        return params;
    }
    
    @Override
    protected void initFilter() {
        cmbTag.removeItemListener(comboboxListener);
        txtBarcode.setText("");
        txtKategori.setText("");
        txtNama.setText("");
        txtPLU.setText("");
        txtPrincipal.setText("");
        txtSupplier.setText("");
        cmbTag.setSelectedIndex(0);
        
        dept = null;
        supplier = null;
        principal = null;
        cmbTag.addItemListener(comboboxListener);
    }

    @Override
    protected void loadData() {
       createNode();
    }

    @Override
    protected void updateGrid() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void insertGrid() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    private void setTableLookStyle() {
        
    }
    
    private boolean saveData(Item item) {
        return ServicesList.getItemService().save(item);
        
    }
   
}
